<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
            <a-form-item :label="$t('门店')">
              <a-select v-model="queryParam.shopCode">
                <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                  >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <j-switch placeholder="请选择状态" v-model="queryParam.status" query></j-switch>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!--              <a @click="handleToggleSearch" style="margin-left: 8px">-->
              <!--                {{ toggleSearchStatus ? '收起' : '展开' }}-->
              <!--                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
              <!--              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="batchEdit('Y')" v-if="selectedRowKeys.length > 0">{{ $t('批量启用') }}</a-button>
      <a-button type="primary" @click="batchEdit('N')" v-if="selectedRowKeys.length > 0">{{ $t('批量停用') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <!--      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
      <!--        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项-->
      <!--        <a style="margin-left: 24px" @click="onClearSelected">清空</a>-->
      <!--      </div>-->

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="rowSelection"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="status" slot-scope="text, record">
          <j-switch v-model="record.status" :disabled="record.code == 'zh_CN'" @change="onChange(record)"></j-switch>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.code != 'zh_CN'">编辑</a>
        </span>
      </a-table>
    </div>

    <bis-sys-language-modal ref="modalForm" @ok="modalFormOk"></bis-sys-language-modal>
  </a-card>
</template>

<script>
import Vue from 'vue'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
import { postAction, httpAction } from '@/api/manage'
import BisSysLanguageModal from './modules/BisSysLanguageModal'

export default {
  name: 'BisSysLanguageList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    BisSysLanguageModal,
  },
  data() {
    return {
      description: 'bis_sys_language管理页面',
      tenantid: Vue.ls.get('TENANT_ID'),
      queryParam: {
        shopCode: '',
        status: '',
      },
      // 表头
      columns: [
        {
          title: '门店编码',
          align: 'center',
          dataIndex: 'tenantId',
        },
        {
          title: '门店名称',
          align: 'center',
          dataIndex: 'shopCode_dictText',
        },
        {
          title: '语言名称',
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: '语言编码',
          align: 'center',
          dataIndex: 'code',
        },
        {
          title: '描述',
          align: 'center',
          dataIndex: 'description',
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
        },
      ],
      url: {
        list: '/system/bisSysLanguage/list',
        delete: '/system/bisSysLanguage/delete',
        deleteBatch: '/system/bisSysLanguage/deleteBatch',
        exportXlsUrl: '/system/bisSysLanguage/exportXls',
        importExcelUrl: 'system/bisSysLanguage/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      shops: [],
    }
  },
  created() {
    this.$set(this.dictOptions, 'status', [
      { text: '是', value: 'Y' },
      { text: '否', value: 'N' },
    ])
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    rowSelection() {
      let _this = this
      const { selectedRowKeys } = this
      return {
        selectedRowKeys,
        onChange: (selectedRowKeys, selectionRows) => {
          _this.onSelectChange(selectedRowKeys, selectionRows)
        },
        getCheckboxProps: (record) => ({
          props: {
            // 全部默认禁止选中
            // disabled: true,
            // 某几项默认禁止选中(R: 当state等于1时)
            disabled: record.code == 'zh_CN', // 某几项默认选中(R: 当state等于1时) // defaultChecked: record.state == 1,
          },
        }),
      }
    },
  },
  methods: {
    batchEdit(status) {
      postAction('/system/bisSysLanguage/editBatch', { ids: this.selectedRowKeys.join(','), status: status }).then(
        (res) => {
          if (res.success) {
            this.searchQuery()
          }
        }
      )
    },
    onChange(record) {
      let that = this
      httpAction('system/bisSysLanguage/edit', record, 'put')
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
          } else {
            that.$message.warning(res.message)
          }
        })
    },
    initDictConfig() {
      initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'int', value: 'tenantId', text: '租户id', dictCode: '' })
      fieldList.push({ type: 'string', value: 'name', text: '语言名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'code', text: '语言编码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'description', text: '描述', dictCode: '' })
      fieldList.push({ type: 'switch', value: 'status', text: '状态' })
      this.superFieldList = fieldList
    },
    searchReset() {
      this.queryParam = {
        shopCode: '',
        status: '',
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>